from QuickerBeta.util2 import *
from pypinyin import pinyin, lazy_pinyin, Style

if "path":
    path1 = r"F:\A_RuanJian\Py_project\pro_test\_others\111.txt"
    path2 = r"F:\A_RuanJian\Py_project\pro_test\_others\222.txt"


# def func1(path1):
#     """
#     1.去重
#     2.查看
#     3.记录
#     """
#     with open(path1, encoding="utf8") as fi:
#         text1 = fi.read()
#     count_1 = len(text1)
#     # 去重， 保持排序
#     text2 = "".join(sorted(set(text1), key=text1.index))
#     count_set = len(text2)
#     # 写入
#     with open(path2, "w", encoding="utf8") as fi:
#         fi.write(text2)
#     return count_1, count_set

def func1(text="成功了"):
    """
    奇怪的bug
        偶尔会有奇怪的韵母，比如 "有"的韵母居然是"iou"
    """
    if "调试":
        print(text)
        exit()
    pinyin_li: list = pinyin(text,
                             style=Style.FINALS,  # 只有韵母
                             # style=Style.NORMAL,  # 无声调
                             # heteronym=True,  # 多音
                             )
    # list (字: 韵母)
    pinyin_str = map("".join, pinyin_li)  # 列表转字符串，对每个元素
    li = list(zip(text, pinyin_str))

    return li


def group_1():
    if """第一次分组""":
        # 处理拼音
        text = read_file(path2)[:]  # 取前x个
        li = func1(text)
        # print(len(li))
        # print(li)
        # exit()
        # 分组
        f_key = lambda e: e[1]
        f_value = lambda e: e[0]
        group_d = group_by_2(li, f_key, f_value)
    if """去除key为单韵母的、空白符的, 排序：key的长度升序""":
        for key in list(group_d.keys()):
            key: str
            if (len(key) == 1) or (key.strip() == ""):
                group_d.pop(key)
        _: tuple = sorted(group_d.items(), key=lambda e: len(e[0]))
    if """处理特殊韵母""":
        _2 = []
        for key, value_li in _:
            key = key.replace("v", "u")  # 把v改成u
            # iou-> ou || uei-> ei ||  uen -> en
            if key == "iou":
                key = "ou"
            if key == "uei":
                key = "ei"
            if key == "uen":
                key = "en"
            _2.append((key, value_li))

        # 把key相同的合并
        d = {}
        for key, value_li in _2:
            d.setdefault(key, []).extend(value_li)
            # if key in d.keys():
            #     d[key] = d[key]+value_li
            # else:
            #     d[key] = value_li
        _ = d
        # return d
    if """提取各种易错韵母""":
        all_keys = """
            1.ao ai an iao 
            2.uan uai ui ai
            3.uai ui ue un
            4.uan  ue  un
            5.in ing ie ei
            6.ang ong iang iong uang
            """.strip().splitlines()
        f_get = lambda key_s: (key_s.strip(),
                               sum([
                                   d[key] for key in
                                   ("ei" if key=="ui" else key
                                    for key in key_s.strip()[2:].split())
                               ], []))
        # g1 = f_get(all_keys[0])
        g_1to6 = dict(map(f_get, all_keys))
    if """提取并打乱顺序""":
        res_li = ["".join(set(v))
                  for v in g_1to6.values()]
    _ = res_li
    return _


if __name__ == '__main__':
    _ = group_1()
    pp()(_)
    # print(len(group_d["iou"]))
    # pp()(group_d["iou"])
    # pp()(func1())
